package com.bjpowernode.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TActivityRemark;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TActivityRemarkMapper;
import com.bjpowernode.query.ActivityRemarkQuery;
import com.bjpowernode.service.ActivityRemarkService;
import com.bjpowernode.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    @Resource
    private TActivityRemarkMapper tActivityRemarkMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public int save(ActivityRemarkQuery activityRemarkQuery) {
        // 创建对象
        TActivityRemark tActivityRemark = new TActivityRemark();
        // 复制
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        // 设置创建时间
        tActivityRemark.setCreateTime(DateTime.now());
        // 设置创建人
        // 解析jwt
        String parseToken = jwtUtil.parseToken(activityRemarkQuery.getJwt());
        TUser user = JSONUtil.toBean(parseToken, TUser.class);
        tActivityRemark.setCreateBy(user.getId());
        // 设置编辑时间
        tActivityRemark.setEditTime(DateTime.now());
        tActivityRemark.setEditBy(user.getId());
        // 删除状态,默认为0
        tActivityRemark.setDeleted(0);
        return tActivityRemarkMapper.insertSelective(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> getActivityByPage(Integer activityId, Integer current) {
        // 开启分页
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 查询
        List<TActivityRemark> activityRemarkList = tActivityRemarkMapper.getActivityByPage(activityId);
        // 封装
        PageInfo<TActivityRemark> pageInfo = new PageInfo<>(activityRemarkList);
        return pageInfo;
    }

    @Override
    public TActivityRemark getActivityById(Integer id) {
        TActivityRemark tActivityRemark = tActivityRemarkMapper.selectByPrimaryKey(id);
        return tActivityRemark;
    }

    @Override
    public int updateActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        // 复制
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        // 设置更新编辑时间
        tActivityRemark.setEditTime(DateTime.now());
        // 设置编辑人
        String parseToken = jwtUtil.parseToken(activityRemarkQuery.getJwt());
        TUser user = JSONUtil.toBean(parseToken, TUser.class);
        tActivityRemark.setEditBy(user.getId());
        // 进行更新操作
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Override
    public int removeActivityRemarkById(Integer id) {
        // 逻辑删除其实是一个更新操作
        TActivityRemark tActivityRemark = new TActivityRemark();
        tActivityRemark.setId(id);
        // 删除状态 , 正常0 , 删除1
        tActivityRemark.setDeleted(1);
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }
}
